﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

//using System.Runtime;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Text.RegularExpressions;

using TranscoderSharp.Coders;

namespace TranscoderSharp
{
    public class LameEncoder
    {
        static string appLame = Path.Combine(Application.StartupPath, "lame.exe");

        //public static void EncodeAudioFiles(IEnumerable<AudioFile> AudioFiles, string OutputDirectory, bool Overwrite, La)
        //{
        //    foreach (AudioFile af in AudioFiles)
        //    {
        //        EncodeAudioFile(af, OutputDirectory, Overwrite);
        //    }
        //}

        /*public static void EncodeAudioFile(AudioFile AudioFileObject)
        {
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();

            StringBuilder sb = new StringBuilder();

            string ConfigString = (AudioFileObject.ConfigObject as LameConfig).GetConfigString();
            sb.Append(ConfigString);


            string FullOutputFilePath = Transcoder.GetOutputFilePath(AudioFileObject);

            if (File.Exists(FullOutputFilePath) && !AudioFileObject.ConfigObject.OverwriteExisting)
            {
                AudioFileObject.Status = AudioFile.EncodingStatus.Skipped;
                return;
            }


            //string OutputFilePath = Transcoder.GetOutputFilePath(AudioFileObject);
            string TempOutputFilePath = Transcoder.GetTempFileName(AudioFileObject);
            


            AudioFileObject.CopyToTempFile();
            sb.Append("\"" + AudioFileObject.TempFilePath + "\" ");
            sb.Append("\"" + TempOutputFilePath + "\" ");

            if (!AudioFileObject.IsValid)
                Transcoder.FixMp3(AudioFileObject);

            int ExitCode = Transcoder.CallExternalProgram(appLame, sb.ToString(), 10);

            if (ExitCode != 0)
            {
                // an error has occured

                AudioFileObject.Status = AudioFile.EncodingStatus.Error;



                StreamWriter sw = new StreamWriter(@"c:\errors.log");
                sw.Write(sb.ToString());
                sw.Close();

                //string error = p.StandardError.ReadToEnd();
               

                //AudioFileObject.DeleteTempFile();

                throw new Exception("Lame exited with " + ExitCode.ToString() + " when trying to " +
                    "encode " + AudioFileObject.FilePath + ".  Lame arguments : " + sb.ToString());
            }

            AudioFileObject.CopyTagToFile(TempOutputFilePath);

            AudioFileObject.Status = AudioFile.EncodingStatus.Encoded;

            if ((AudioFileObject.ConfigObject as LameConfig).ApplyMp3Gain)
                Transcoder.ApplyMp3Gain(TempOutputFilePath, AudioFileObject.Timeout);

            File.Copy(TempOutputFilePath, FullOutputFilePath, true);
            File.Delete(TempOutputFilePath);

            //AudioFileObject.DeleteTempFile();

            AudioFileObject.EncodingTime = stopWatch.Elapsed;
        }*/
    }
}
